package com.voxel.sense.infomsg.module.task.execution.report.list;

import com.example.com.common.app.BasePresenter;
import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.TaskReportListEntity;
import cyy.example.com.facotry.net.RequestIdFactory;
import cyy.example.com.facotry.net.helper.TaskNetHelper;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des :
 */
public class ReportListPresenter extends BasePresenter<ReportListContract.View>
        implements ReportListContract.Presenter, DataSource.Callback<BaseEntity<List<TaskReportListEntity>>> {

    //记录操作的表单
    private String mFormCode;

    @Override
    public void start() {
        super.start();
        EventBus.getDefault().register(this);

    }

    @Override
    public void destroy() {
        super.destroy();
        EventBus.getDefault().unregister(this);
    }

    public ReportListPresenter(ReportListContract.View view) {
        super(view);
    }

    @Override
    public void doDeleteExistReport(String taskNumb, String formCode) {
        mView.showLoading("");
        this.mFormCode = formCode;
        TaskNetHelper.cleanReportResult(taskNumb, formCode, this);
    }

    @Override
    public void doLoadReportList(String taskNumb) {
        TaskNetHelper.getTaskReportList(taskNumb, this);
    }

    @Override
    public void onDataLoaded(BaseEntity<List<TaskReportListEntity>> data, int reqId) {
        if (mView == null) {
            return;
        }

        if (reqId == RequestIdFactory.GET_TASK_REPORT) {
            if (data.isSuccess()) {
                List<TaskReportListEntity> reportList = data.getData();
                mView.getRecyclerViewAdapter().replace(reportList);
            }
            mView.onAdapterDataChanged();
        } else if (reqId == RequestIdFactory.CLEAN_TASK_REPORT_RESULT) {
            cleanReportResultSuccess(data);
        }
    }


    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if (reqId == RequestIdFactory.GET_TASK_REPORT) {
            mView.onAdapterDataChanged();
        } else {
            mView.dimissLoading();
            ToastUtil.showToastLong(Factory.app(), "清除表格信息失败");
        }
    }

    /**
     * 清除成功，进行刷新数据及界面
     */
    private void cleanReportResultSuccess(BaseEntity<List<TaskReportListEntity>> data) {
        mView.dimissLoading();
        if (data.isSuccess()) {
            List<TaskReportListEntity> items = mView.getRecyclerViewAdapter().getItems();
            //清除表格后，这里要进行判断是否还有已上传的表格存在，然后发通知。
            boolean flag = false;
            for (TaskReportListEntity entity : items) {
                if (entity.getFormCode().equals(mFormCode)) {
                    entity.setParam2(false + "");
                    mView.getRecyclerViewAdapter().notifyDataSetChanged();
                }

                if (entity.getParam2().equals(true + "")) {
                    flag = true;
                }
            }
            if (!flag) {
                EventBus.getDefault().post(new EventEntity(EventBusConstants.TASK_RESULT_REPORT_DELLETE, ""));
            }
        } else {
            ToastUtil.showToastLong(Factory.app(), "清除表格信息失败");
        }
    }

    @Subscribe
    public void onEventBusThread(EventEntity entity) {
        //收到表格上传成功的通知
        if (EventBusConstants.UP_REPORT_SUCCESS.equals(entity.key) && entity.value instanceof String) {
            String formCode = (String) entity.value;
            List<TaskReportListEntity> items = mView.getRecyclerViewAdapter().getItems();
            for (int i = 0; i < items.size(); i++) {
                TaskReportListEntity reportListEntity = items.get(i);
                if (reportListEntity.getFormCode().equals(formCode)) {
                    reportListEntity.setParam2(true + "");
                    mView.getRecyclerViewAdapter().notifyItemChanged(i);
                    return;
                }
            }
        }
    }

}
